package carAgents;


import jade.core.AID;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class OfferAgentGui extends JFrame {	
	private OfferAgent myAgent;
	
	private JTextField startPointCountry, startPointCity, startPointStreet;
	private JTextField goalPointCountry, goalPointCity, goalPointStreet;
	
	OfferAgentGui(OfferAgent a) {
		super(a.getLocalName());
		
		myAgent = a;
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(5, 3));
		p.add(new JLabel());
		p.add(new JLabel("Start Point:"));
		p.add(new JLabel("Goal Point:"));
		p.add(new JLabel("Country:"));
		startPointCountry = new JTextField(15);
		goalPointCountry = new JTextField(15);
		p.add(startPointCountry);
		p.add(goalPointCountry);
		p.add(new JLabel("City:"));
		startPointCity = new JTextField(15);
		goalPointCity = new JTextField(15);
		p.add(startPointCity);
		p.add(goalPointCity);
		p.add(new JLabel("Street:"));
		startPointStreet = new JTextField(15);
		goalPointStreet = new JTextField(15);
		p.add(startPointStreet);
		p.add(goalPointStreet);
		p.add(new JLabel());
		p.add(new JLabel());
		p.add(new JLabel());
		
		getContentPane().add(p, BorderLayout.CENTER);
		
		JButton addButton = new JButton("Offer");
		addButton.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					Point startP = new Point(startPointCountry.getText().trim(),
							startPointCity.getText().trim(),
							startPointStreet.getText().trim());
					
					Point goalP = new Point(goalPointCountry.getText().trim(),
							goalPointCity.getText().trim(),
							goalPointStreet.getText().trim());
					
					myAgent.updateCatalogue(startP, goalP);
					startPointCity.setText("");
					startPointCountry.setText("");
					startPointStreet.setText("");
					goalPointCountry.setText("");
					goalPointCity.setText("");
					goalPointStreet.setText("");
				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(OfferAgentGui.this, "Wrong values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
				}
			}
		} );
		p = new JPanel();
		p.add(addButton);
		getContentPane().add(p, BorderLayout.SOUTH);
		
		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				myAgent.doDelete();
			}
		} );
		
		setResizable(false);
	}
	
	public void display() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		setVisible(true);
	}	
}
